#include <QPainter>
#include "traffic_sim.h"
#include "semaphore.h"
#include "edge.h"
#include "node.h"
#include "agent.h"
#include "graphwidget.h"
#include <iostream>
#include <math.h>


using namespace std;
using namespace Traffic_Sim;
Semaphore::Semaphore(Node * Node)
{
  node = Node;	
}

Semaphore::~Semaphore()
{
}

void Semaphore::setSemaphore(Traffic_Semaphore * sem)
{
  semaphore = sem;
}

QRectF Semaphore::boundingRect() const
{
  return QRectF(10,10 ,-30,-30);
}

void Semaphore::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
  for (int i = 0; i <node->edgeList.size(); ++i)
    {
      for (int j = 0; j < semaphore->get_arc_count(); j++)
	{ 
 	   Traffic_Arc * arc = semaphore->get_arc(j);
	  if(node->edgeList[i]->info_arc.description==arc->get_info().description){
	    Semaphore_Color color = semaphore->get_color_to_show(j);
	    Traffic_Arc * arc = semaphore->get_arc(j);
	    switch (color)
	      {
	      case Green:
		if(node==node->edgeList[i]->source)
		  {
		    painter->setBrush(Qt::green);
		    painter->drawEllipse(node->edgeList[i]->sourcePoint.x(),node->edgeList[i]->sourcePoint.y(),10,10);		
		  }
		if(node==node->edgeList[i]->dest)
		  {
		    painter->setBrush(Qt::green);
		    painter->drawEllipse(node->edgeList[i]->destPoint.x(),node->edgeList[i]->destPoint.y(),10,10);
		  }			
		break;
	      case Yellow:
		if(node==node->edgeList[i]->source)
		  {
		    painter->setBrush(Qt::yellow);
		    painter->drawEllipse(node->edgeList[i]->sourcePoint.x(),node->edgeList[i]->sourcePoint.y(),10,10);		
		  }
		if(node==node->edgeList[i]->dest)
		  {
		    painter->setBrush(Qt::yellow);
		    painter->drawEllipse(node->edgeList[i]->destPoint.x(),node->edgeList[i]->destPoint.y(),10,10);
		  }			
		break;
	      case Red:
		if(node==node->edgeList[i]->source)
		  {
		    painter->setBrush(Qt::red);
		    painter->drawEllipse(node->edgeList[i]->sourcePoint.x(),node->edgeList[i]->sourcePoint.y(),10,10);		
		  }
		if(node==node->edgeList[i]->dest)
		  {
		    painter->setBrush(Qt::red);
		    painter->drawEllipse(node->edgeList[i]->destPoint.x(),node->edgeList[i]->destPoint.y(),10,10);
		  }			
		break;
	      }  
	
	  }
	  
	}
    }
update();
}
